package com.spring.springcloud.Elasticsearch;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class B_ES的操作 {
    public static void main(String[] args) throws IOException {

        //在 Elasticsearch  8.x的版本已过期
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );
        String sy = "user";//对此索引操作

//        插入数据(esClient, sy);
//        修改数据(esClient, sy);
//        查询数据(esClient, sy);
//        删除数据(esClient, sy);


        esClient.close();//关闭客户端
    }

    private static void 删除数据(RestHighLevelClient esClient, String sy) throws IOException {
        //删除数据
        DeleteRequest request = new DeleteRequest();
        request.index(sy).id("1003");
        DeleteResponse delete = esClient.delete(request, RequestOptions.DEFAULT);
        System.out.println(delete);
    }

    private static void 查询数据(RestHighLevelClient esClient, String sy) throws IOException {
        //查询数据
        GetRequest gr = new GetRequest();
        gr.index(sy).id("1001");
        //访问
        GetResponse documentFields =
                esClient.get(gr, RequestOptions.DEFAULT);

        String sourceAsString = documentFields.getSourceAsString();
        System.out.println("----------------------------------");
        System.out.println(sourceAsString);
        System.out.println("----------------------------------");
    }

    private static void 修改数据(RestHighLevelClient esClient, String sy) throws IOException {
        //修改数据
        UpdateRequest ur = new UpdateRequest();
        //修改的指定数据
        ur.index(sy).id("1001");
        //修改的具体属性
        ur.doc(XContentType.JSON,"sex","女");
        //执行
        UpdateResponse update =
                esClient.update(ur, RequestOptions.DEFAULT);

        DocWriteResponse.Result result = update.getResult();
        System.out.println(result);
    }

    private static void 插入数据(RestHighLevelClient esClient, String sy) throws IOException {
        //插入数据
        IndexRequest indexRequest = new IndexRequest();
        indexRequest.index(sy).id("1003");

        User user = new User();
        user.setName("张三2");
        user.setAge(22);
        user.setSex("男");

        //由于es是JSON格式，需要转 JSON 格式
        ObjectMapper om = new ObjectMapper();
        String userJson = om.writeValueAsString(user);

        //放入请求体
        indexRequest.source(userJson, XContentType.JSON);

        IndexResponse index =
                esClient.index(indexRequest, RequestOptions.DEFAULT);

        System.out.println(index.getResult());
    }

}
